package com.anysoft.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.Button;
import android.widget.TextView;

import com.anysoft.R;

public class RegisterSuccessDialog extends Dialog implements View.OnClickListener {

    private TextView tv_dialog_register_risk_level;

    private Button btn_dialog_register_risk_level;

    private View mDialogView;

    private AnimationSet mAnimIn, mAnimOut;

    private OnNegativeListener mNegativeListener;

    private String mContentText;

    private boolean mIsShowAnim;

    public RegisterSuccessDialog(Context context, String mContentText) {
        this(context, 0);
        this.mContentText = mContentText;
    }

    public RegisterSuccessDialog(Context context, int theme) {
        super(context, R.style.color_dialog);
        init();
    }

    private void callDismiss() {
        super.dismiss();
    }

    private void init() {
        mAnimIn = AnimationLoader.getInAnimation(getContext());
        mAnimOut = AnimationLoader.getOutAnimation(getContext());
        initAnimListener();
    }

    @Override
    public void setTitle(int titleId) {
        setTitle(getContext().getText(titleId));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View contentView = View.inflate(getContext(), R.layout.dialog_register_success, null);
        setContentView(contentView);

        mDialogView = getWindow().getDecorView().findViewById(android.R.id.content);
        tv_dialog_register_risk_level = (TextView) contentView.findViewById(R.id.tv_dialog_register_risk_level);
        btn_dialog_register_risk_level = (Button) contentView.findViewById(R.id.btn_dialog_register_risk_level);

        tv_dialog_register_risk_level.setText(mContentText);

        btn_dialog_register_risk_level.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        startWithAnimation(mIsShowAnim);
    }

    @Override
    public void dismiss() {
        dismissWithAnimation(mIsShowAnim);
    }

    private void startWithAnimation(boolean showInAnimation) {
        if (showInAnimation) {
            mDialogView.startAnimation(mAnimIn);
        }
    }

    private void dismissWithAnimation(boolean showOutAnimation) {
        if (showOutAnimation) {
            mDialogView.startAnimation(mAnimOut);
        } else {
            super.dismiss();
        }
    }

    private void initAnimListener() {
        mAnimOut.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mDialogView.post(new Runnable() {
                    @Override
                    public void run() {
                        callDismiss();
                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (R.id.btn_dialog_register_risk_level == id) {
            //System.out.println("R.id.btn_dialog_register_risk_level == id");
            mNegativeListener.onClick(this);
        } else {
        }
    }

    public RegisterSuccessDialog setAnimationEnable(boolean enable) {
        mIsShowAnim = enable;
        return this;
    }

    public RegisterSuccessDialog setAnimationIn(AnimationSet animIn) {
        mAnimIn = animIn;
        return this;
    }

    public RegisterSuccessDialog setAnimationOut(AnimationSet animOut) {
        mAnimOut = animOut;
        initAnimListener();
        return this;
    }

    public interface OnNegativeListener {
        void onClick(RegisterSuccessDialog dialog);
    }

    public RegisterSuccessDialog setNegativeListener(OnNegativeListener l) {
        mNegativeListener = l;
        return this;
    }

}
